/**
 * 上传图片预览
 * @param file
 * @param imghead
 * @param preview
 * @param checkChooseFileId 检测是否有选择文件的隐藏域的id
 */
function previewImage(file, imghead, preview, checkChooseFileId) {
	if(!file) {
		return;
	}
	var MAXWIDTH = 200;
	var MAXHEIGHT = 100;
	var div = document.getElementById(preview);
	if (file.files && file.files[0]) {
		div.innerHTML = '<img id='+imghead+'>';
		var img = document.getElementById(imghead);
		img.onload = function() {
			var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT,
					img.offsetWidth, img.offsetHeight);
			img.width = rect.width;
			img.height = rect.height;
			img.style.marginLeft = rect.left + 'px';
			img.style.marginTop = rect.top + 'px';
		}
		var reader = new FileReader();
		reader.onload = function(evt) {
			img.src = evt.target.result;
		}
		reader.readAsDataURL(file.files[0]);
	} else {
		var sFilter = 'filter:'+imghead+'id:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
		file.select();
		var selection = document.selection;
		if(!selection) {
			return;
		}
		var src = selection.createRange().text;
		div.innerHTML = '<img id='+imghead+'>';
		var img = document.getElementById(imghead);
		img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
		var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth,
				img.offsetHeight);
		status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width
				+ ',' + rect.height);
		div.innerHTML = "<div  style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;margin-left:"+rect.left+"px;"+sFilter+src+"\"'></div>";
	}
	//如果是添加页面，该隐藏域的内容为空，所以选择完图片后，把隐藏域的值设置为1；如果是修改页面，该隐藏域的值为图片路径
	var chooseFileVal = $(checkChooseFileId).val();
	if(!chooseFileVal) {
		$(checkChooseFileId).val(1);
		$(checkChooseFileId).next().css({display: 'none'}); //隐藏提示信息
	}
}
/**
 * 图片缩放
 * @param maxWidth
 * @param maxHeight
 * @param width
 * @param height
 * @returns {___anonymous44737_44803}
 */
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
	var param = {
		top : 0,
		left : 0,
		width : width,
		height : height
	};
	if (width > maxWidth || height > maxHeight) {
		rateWidth = width / maxWidth;
		rateHeight = height / maxHeight;
		if (rateWidth > rateHeight) {
			param.width = maxWidth;
			param.height = Math.round(height / rateWidth);
		} else {
			param.width = Math.round(width / rateHeight);
			param.height = maxHeight;
		}
	}
	param.left = Math.round((maxWidth - param.width) / 2);
	param.top = Math.round((maxHeight - param.height) / 2);
	return param;
}

function imgZomm(imghead){
	var MAXWIDTH = 200;
	var MAXHEIGHT = 100;
	var img = document.getElementById(imghead);
	img.onload = function() {
		var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT,
				img.offsetWidth, img.offsetHeight);
		img.width = rect.width;
		img.height = rect.height;
		img.style.marginLeft = rect.left + 'px';
		img.style.marginTop = rect.top + 'px';
	};
	var reader = new FileReader();
	reader.onload = function(evt) {
		img.src = evt.target.result;
	};
}
 